package com.anjiplus.template.gaea.business.modules.quartz.service.impl;

import com.anjiplus.template.gaea.business.modules.quartz.service.UtilService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UtilServiceImpl implements UtilService {

        public String getTs(String methodName, JdbcTemplate jdbcTemplate) {String ts = "2024-01-01 00:00:00";
        //查询sql
        String tsSql = "select DATE_FORMAT(yyts, '%Y-%m-%d %H:%i:%s') AS yyts from gs_yy_log where methodName = '"+methodName+"' order by yyts desc limit 1  ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if(maps.size()>0){
            ts= (String) maps.get(0).get("yyts");
            if(ts==null||ts.equals("")){
                ts= "2024-01-01 00:00:00";
            }
        }else{
            ts= "2024-01-01 00:00:00";
        }
        return ts;
    }
    /**
     * 根据中间库的org
     * 确定查询范围
     * */
    @Override
    public String getGSOrgStr(JdbcTemplate jdbcTemplate) {
        String orgStr = "";
        String tsSql = "SELECT GROUP_CONCAT(yyCode SEPARATOR '&') as orgStr FROM yy_org_orgs where gsCode is not null";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if(maps.size()>0){
            orgStr= (String) maps.get(0).get("orgStr");
            //替换，  变成 ','
            orgStr = "'"+orgStr+"'";
            orgStr = orgStr.replace("&","','");
        }else{
            orgStr= "";
        }
        return orgStr;

    }

    @Override
    public String getItemTs(String methodName,String itemClass, JdbcTemplate jdbcTemplate) {
        String ts = "";
        //查询sql
        String tsSql = "select yyts from gs_yy_log where methodName = '"+methodName+"'and itemClass = '"+itemClass+"' order by yyts desc limit 1  ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if(maps.size()>0){
            ts= (String) maps.get(0).get("yyts");
            if(ts==null||ts.equals("")){
                ts= "2024-01-01 00:00:00";
            }
        }else{
            ts= "2024-01-01 00:00:00";
        }
        return ts;
    }

}
